package top.yzlin.jqueryjx3strategystation.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.yzlin.jqueryjx3strategystation.dao.ArticleDAO;
import top.yzlin.jqueryjx3strategystation.dao.CommentDAO;
import top.yzlin.jqueryjx3strategystation.entity.community.Comment;
import top.yzlin.jqueryjx3strategystation.entity.user.User;
import top.yzlin.jqueryjx3strategystation.entity.vo.ResponseVO;


@RestController
public class CommentController {

    private CommentDAO commentDAO;
    private ArticleDAO articleDAO;

    @Autowired
    public void setArticleDAO(ArticleDAO articleDAO) {
        this.articleDAO = articleDAO;
    }

    @Autowired
    public void setCommentDAO(CommentDAO commentDAO) {
        this.commentDAO = commentDAO;
    }

    @PostMapping("/comment/save")
    public ResponseVO commentSave(@RequestParam("articleId")Integer articleId,
                                  @SessionAttribute("user") User user,
                                  Comment comment){
        return articleDAO.findById(articleId).map(article -> {
            comment.setArticle(article);
            comment.setUser(user);
            commentDAO.save(comment);
            return ResponseVO.SUCCESS;
        }).orElse(ResponseVO.NOT_FOUND);
    }

    @GetMapping("/comment/{articleId:\\d+}")
    public ResponseVO commentList(@PathVariable("articleId") Integer articleId) {
        return ResponseVO.createSuccessInstance(commentDAO.findCommentByArticleId(articleId));
    }

}
